using System.Collections;
using System.IO;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using Point = System.Drawing.Point;
using Size = System.Drawing.Size;

public class login : MonoBehaviour
{
    GameObject _root;
    #nullable enable
    GameObject? _footer;
    Camera _camera;
    Material _outlineMaterial;
    Material _petrifaction;
    GameObject _fileInput;
    AudioSource _sound;
    string _input = string.Empty;


    //音乐

    private void Awake()
    {   
        _root = GameObject.Find("Canvas");
        var camera = GameObject.Find("Main Camera");
        _camera = camera.GetComponent<Camera>();
        // 描边shader
        _outlineMaterial = new Material(Shader.Find("shaders/outline"));
        _petrifaction = new Material(Shader.Find("shaders/petrifaction"));
        _fileInput = GameObject.Find("InputField");
        _fileInput.SetActive(false);
        CustInputStyle();
        var obj = new GameObject();
        _sound = obj.AddComponent<AudioSource>();

    }

    // Start is called before the first frame update
    void Start()
    {
        _outlineMaterial.SetColor("_lineColor", Color.yellow);
        _outlineMaterial.SetInt("_lineWidth", 2);
        _petrifaction.SetTexture("_StatueTex", GetTexture("petrifaction.png", new Size(512, 512)));
        _petrifaction.SetFloat("_Brightness", 1f);
        _petrifaction.SetFloat("_StatueDegree", 0.7f);

#if !DEBUG
        if (Screen.width != GameObjectEx.GameSize.Width)
        {
            GameObjectEx.GameSize = new Size(Screen.width, Screen.height);
        }
#endif

        _camera.orthographicSize = GameObjectEx.Size;
        AddImage("back.jpg", new Size(1920, 873), Point.Empty);
        AddImage("banner.jpg", new Size(1920, 694), new Point(0, 100));
        AddImage("Snapshot.jpg", new Size(720, 1480), new Point(20, 0));
        AddImage("monster.png", new Size(800, 1005), new Point(0, 100));
        AddImage("monster.png", new Size(800, 1005), new Point(801, 100), _outlineMaterial);
        AddImage("monster.png", new Size(800, 1005), new Point(1602, 100), _petrifaction);

        _footer = AddImage("banner.jpg", new Size(1920, 694), new Point(0, 0));
        ChangeLocation();
        StartCoroutine(ChangeSound("mir.mp3"));

    }

    IEnumerator ChangeSound(string path)
    {
        //获取.wav文件，并转成AudioClip
        UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file:///" + Path.Combine(Consts.BaseUrl,"Sound",path), AudioType.MPEG);
        //等待转换完成
        yield return www.SendWebRequest();
        //获取AudioClip
        AudioClip audioClip = DownloadHandlerAudioClip.GetContent(www);
        //设置当前AudioSource组件的AudioClip
        _sound.clip = audioClip;
        _sound.loop = true;
        //播放声音
        _sound.Play();
    }

    void CustInputStyle()
    {
        //背景色
        var image = _fileInput.GetComponent<Image>();
        image.color = Color.black;
        //清空待输入提示
        var input = _fileInput.GetComponent<TMP_InputField>();
        input.placeholder.GetComponent<TextMeshProUGUI>().text = "";
        //字体样式
        var text = input.textComponent;
        text.color = Color.white;
        text.outlineWidth = 0f;
        text.outlineColor = Color.black;
        // 获取InputField中的gui对象，它包含了光标
        var gui = text.GetComponentInChildren<TextMeshProUGUI>();
        // 修改它的颜色
        if (gui != null)
        {
            // 获取并修改Material的颜色属性
            gui.material.SetColor("_Color", Color.white);
        }
    }
    void ChangeLocation()
    {
        _footer?.ChangePosition(new Point(0, GameObjectEx.GameSize.Height - 694));
        _fileInput.ChangePosition(100, GameObjectEx.GameSize.Height - 100);
    }
    // Update is called once per frame
    void Update()
    {
       
        if (Input.GetKeyDown(KeyCode.Return)||Input.GetKeyDown(KeyCode.KeypadEnter))
        {
            Screen.fullScreen = !Screen.fullScreen;
        }
        else if (Input.GetKeyDown(KeyCode.F1))
        {
            GameObjectEx.ChangeSize(new Size(1024, 768));
            ChangeLocation();
        }
        else if (Input.GetKeyDown(KeyCode.F2))
        {
            GameObjectEx.ChangeSize(new Size(1920, 1080));
            ChangeLocation();
        }
        else if (Input.GetKeyDown(KeyCode.F3))
        {
            GameObjectEx.ChangeSize(new Size(2560, 1600));
            ChangeLocation();
        }
        else if (Input.GetKeyDown(KeyCode.F4))
        {
            _fileInput.SetActive(!_fileInput.activeSelf);
            ChangeLocation();
        }

    }

    private GameObject AddImage(string path,Size size,Point location)
    {
        var img = new GameObject(Path.GetFileName(path) + GameObjectEx.sort);
        var spirte = Sprite.Create(GetTexture(path, size), new Rect(Vector2.zero, ToVector2(size)), new Vector2(0, 1));
        img.transform.parent = _root.transform;
        img.transform.position = new Vector3(0, 0);
        img.AddComponent<RectTransform>();
        img.AddComponent<SpriteRenderer>().sprite = spirte;
        img.Location(location);
        return img;
    }
    private GameObject AddImage(string path, Size size, Point location,Material material)
    {
        var img = AddImage(path, size, location);
        img.GetComponent<SpriteRenderer>().material = material;
        return img;
    }

    #nullable enable
    private Texture2D? GetTexture(string imgPath,Size size)
    {

        var path = Path.Combine(Consts.BaseUrl, "Data",imgPath);
        if (!File.Exists(path))
        {
            return null;
        }
        var texture = new Texture2D(size.Width,size.Height);
        texture.LoadImage(File.ReadAllBytes(path));
        texture.Apply();
        return texture;
    }

    private Vector2 ToVector2(Size size)
    {
        return new Vector2(size.Width, size.Height);
    }

    private void OnApplicationQuit()
    {
        //释放资源

    }
    private void OnGUI()
    {
        _input = GUI.TextField(new Rect { x = 20, y = 20, width = 200, height = 20 }, _input);
    }
}